﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using YFIN.View.FileServiceRef;
using YFIN.View.ViewModels;

namespace YFIN.View.Lockbox
{
    public partial class File : UserControl
    {
        private FileVM _fvm;
        public File()
        {
            InitializeComponent();
            _fvm = new FileVM();
            DataContext = _fvm;
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _fvm.SetCurrent(((ListBox) sender).SelectedIndex);
        }

        private bool _isPasteEnabled = false;
        private void button6_Click(object sender, RoutedEventArgs e)
        {
            CutFile();
        }

        private void CutFile()
        {
            if (_fvm.CurrentFile != null)
            {
                _isPasteEnabled = true;
                btnUpdate.Opacity = 1;
                btnUpdate.MouseEnter += imageBtn_MouseEnter;
                btnUpdate.MouseLeave += imageBtn_MouseLeave;
                _fvm.CutFile = _fvm.CurrentFile;
            }
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            PasteFile();
        }

        private void PasteFile()
        {
            if (_isPasteEnabled)
            {
                _isPasteEnabled = false;
                btnUpdate.Opacity = 0.5;
                btnUpdate.MouseEnter -= imageBtn_MouseEnter;
                btnUpdate.MouseLeave -= imageBtn_MouseLeave;
                _fvm.Update();
            }
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            ((Image)sender).Source = new BitmapImage(new Uri("/YFIN.View;component/Images/file/fail.png", UriKind.Relative));
        }

        private void imageBtn_MouseEnter(object sender, MouseEventArgs e)
        {
            ((Image) sender).Opacity = 0.7;
        }

        private void imageBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            ((Image) sender).Opacity = 1;
        }

        private DateTime _prev = DateTime.MinValue;
        private ModelFile _file = null;
        private void WrapPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var now = DateTime.Now;
            if((now - _prev).TotalSeconds > 1 || _file != _fvm.CurrentFile)
            {
                _prev = now;
                _file = _fvm.CurrentFile;
                return;
            }
            if(_fvm.CurrentFile.Type)
            {
                _fvm.DownloadFile();
            }
            else
            {
                _fvm.OpenDirectory();
            }
            _prev = DateTime.MinValue;
        }

        private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.X && Keyboard.Modifiers == ModifierKeys.Control)
            {
                CutFile();
            }
            else if(e.Key == Key.V && Keyboard.Modifiers == ModifierKeys.Control)
            {
                PasteFile();
            }
            else if(e.Key == Key.Back)
            {
                _fvm.BackDirectory();
            }
            else if(e.Key == Key.Delete)
            {
                _fvm.Delete();
            }
            else if(e.Key == Key.F2)
            {
                _fvm.Rename();
            }
        }
    }
}
